using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.U2D;
using YooAsset;
using System.Collections.Generic;
using Client.Base;
namespace Client.Hotfix
{
    public class YooAssetLoader : ILoader
    {
        private readonly Dictionary<string, ResourcePackage> pks = new();
        public YooAssetLoader()
        {
            foreach (var pkName in AppInfo.Packages)
            {
                if (!pks.TryGetValue(pkName, out var pk))
                {
                    pk = YooAssets.GetPackage(pkName);
                    if (pk == null)
                    {
                        Debug.Log($"[YooAssetLoader] {pk} not exist");
                    }
                    else
                    {
                        pks.Add(pkName, pk);
                    }
                }

            }
        }
        public async UniTask<SpriteAtlas> LoadAtlasSync(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                var handle = pks[AppInfo.GamePackage].LoadAssetAsync<SpriteAtlas>(location);
                await handle.ToUniTask();
                return handle.AssetObject as SpriteAtlas;
            }
            else
            {
                return null;
            }
        }

        public async UniTask<TextAsset> LoadConfigSync(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                var handle = pks[AppInfo.GamePackage].LoadAssetAsync<TextAsset>(location);
                await handle.ToUniTask();
                var ta = handle.AssetObject as TextAsset;
                handle.Release();
                return ta;
            }
            else
            {
                return null;
            }
        }

  

        public async UniTask<GameObject> LoadPrefabSync(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                var handle = pks[AppInfo.GamePackage].LoadAssetAsync<GameObject>(location);
                await handle.ToUniTask();
                GameObject clone = GameObject.Instantiate(handle.AssetObject) as GameObject;
                return clone;
            }
            else
            {
                return null;
            }
        }

        public async UniTask<AudioClip> LoadSoundSync(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                var handle = pks[AppInfo.GamePackage].LoadAssetAsync<AudioClip>(location);
                await handle.ToUniTask();
                return handle.AssetObject as AudioClip;
            }
            else
            {
                return null;
            }
        }

        public async UniTask<Texture2D> LoadTexture2DSync(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                var handle = pks[AppInfo.GamePackage].LoadAssetAsync<Texture2D>(location);
                await handle.ToUniTask();
                return handle.AssetObject as Texture2D;
            }
            else
            {
                return null;
            }
        }
        public async UniTask<bool> LoadSceneSync(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                var handle = pks[AppInfo.GamePackage].LoadAssetAsync(location);
                await handle.ToUniTask();
                return true;
            }
            else
            {
                return false;
            }
        }
        public async UniTask<string> LoadVideoSync(string location)
        {
            //if (pks[AppInfo.RawPackage] != null)
            //{
            //    var handle = pks[AppInfo.RawPackage].LoadRawFileAsync(location);
            //    await handle.ToUniTask();
            //    return handle.GetRawFilePath();
            //}
            //else
            //{
            //    return string.Empty;
            //}
            return null;
        }

        public async void UnloadUnusedAssetsAsync()
        {
            foreach (var item in pks)
            {
                await item.Value.UnloadUnusedAssetsAsync();
            }
        }

        public void ReleaseAsset(string location)
        {
            if (pks[AppInfo.GamePackage] != null)
            {
                pks[AppInfo.GamePackage].TryUnloadUnusedAsset(location);
            }
        }

        public void Destroy()
        {
            pks.Clear();
        }
    }
}

